package sap.gui;

import java.util.List;
import javax.swing.tree.*;
import javax.swing.tree.DefaultMutableTreeNode;

import data.*;

/*
 * CManager.java
 *
 * Created on 10 de noviembre de 2007, 12:13 PM
 */

/**
 *
 * @author  Enrique Garc�a
 */
public class CManager extends javax.swing.JFrame{
    
    /** Creates new form CManager */
    public CManager() {
        try
      {
      javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
      instance = this;
      } 
      catch(Exception e) {}    
        
        this.initComponents();
        this.initLogin();
        this.initMainIcon();
        this.centerLocation();
        this.setResizable(true);
        this.setSize(1015,725);

        
    }
    
    private void centerLocation()
    {
     
    int maxHeight = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight();
    int maxWidth = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth();
    int midleX= maxWidth/2;
    int midleY= maxHeight/2;
    this.setLocation((midleX-(792/2)),(midleY-(560/2)));
        
    }
    
    private void initMainIcon()
    {
    //icon=tools.getDefaultToolkit().getImage(getClass().getResource("Icons/classroom.png"));
    //this.setIconImage(icon);
    }
    
    
    protected void initTabs()
    {
        p_man=new ProjectManager(this);
        psp_man=new PSPManager(this);
        mile_man=new MilestonManager(this);
        gant=new GantTable(this);
        jTabbedPane1.addTab("Project Manager",tab1I,p_man);
        jTabbedPane1.addTab("PSP-Manager",tab3I,psp_man);
        jTabbedPane1.addTab("Milestone Manager",tab5I,mile_man);
        jTabbedPane1.addTab("Gantt Table",tab1I,gant);

        jTabbedPane1.setSelectedIndex(0);
    }

    public  void reloadP_man(String project)
    {
        p_man.init(project);
        p_man.clear();

    }

    protected void reloadPsp_man(String psp)
    {
        psp_man.init(psp);
        p_man.clear();

    }

    protected void reloadMile_man()
    {
        mile_man.initC();
        mile_man.clear();
    }

    private void initLogin()
    {
        login=new LoginManager(this);
        jTabbedPane1.addTab("Login",tab4I,login);
    }

    private void reloadGant()
    {
        gant.init(project);
    }


    protected void treeModel(List<PSPHierarchy> project, String p_def)
    {

        final DefaultTreeModel model = (DefaultTreeModel)Tree.getModel();
        DefaultMutableTreeNode treeNode =
                       new DefaultMutableTreeNode(p_def);
        model.setRoot(treeNode);
        jTreeBuild(project, treeNode);

    }

    public static void jTreeBuild(List<PSPHierarchy> nodes, DefaultMutableTreeNode parent) {
            if (nodes.isEmpty())
            {}
            else
            {
                for(int j=0;j<nodes.size();j++)
                {
                    DefaultMutableTreeNode treeNode =
                       new DefaultMutableTreeNode(nodes.get(j).wbs_element);
                    // process children
                    parent.add(treeNode);

                       jTreeBuild(nodes.get(j).children, treeNode);
                }
            } 
    }


    public void hideTabs()
    {
        //reload the Project Manager Tab
        
        jTabbedPane1.removeAll();
        jTabbedPane1.addTab("Login",tab4I,login);

        //
    }

    public void showTabs()
    {
        //Show Project Manager Tab
        jTabbedPane1.addTab("Project Manager",tab1I,p_man);
        jTabbedPane1.addTab("PSP-Manager",tab3I,psp_man);
        jTabbedPane1.addTab("Milestone Manager",tab5I,mile_man);
    }



   

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tree = new javax.swing.JTree();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenuBar2.setBackground(new java.awt.Color(0, 51, 51));
        jMenuBar2.setForeground(new java.awt.Color(204, 204, 204));

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SAP Project GUI");
        setBackground(new java.awt.Color(0, 0, 0));
        getContentPane().setLayout(null);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setLayout(null);

        jTabbedPane1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.add(jTabbedPane1);
        jTabbedPane1.setBounds(160, 0, 850, 660);

        Tree.setBackground(new java.awt.Color(0, 0, 0));
        Tree.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Project Structure", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        Tree.setForeground(new java.awt.Color(255, 255, 255));
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        Tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(Tree);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(0, 0, 160, 660);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 1010, 690);

        jMenuBar1.setDoubleBuffered(true);
        jMenuBar1.setEnabled(false);

        jMenu1.setBorder(null);
        jMenu1.setText("Select");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sap/gui/Icons/search.png"))); // NOI18N
        jMenuItem1.setText("Select Project");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sap/gui/Icons/options.png"))); // NOI18N
        jMenuItem2.setText("Select a PSP");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.project = javax.swing.JOptionPane.showInputDialog(this,
                "Type the project name", "Project Selection",
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if(project.equals(null))
        {
            javax.swing.JOptionPane.showMessageDialog(this,"Empty definition is not possible","Error",1);
        }
        else
        {
            this.reloadP_man(project);
            new F4Dialog(project);
            jTabbedPane1.setSelectedComponent(p_man);
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        this.psp = javax.swing.JOptionPane.showInputDialog(this,
                "Type the PSP name", "PSP Selection",
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if(psp.equals(null))
        {
            javax.swing.JOptionPane.showMessageDialog(this,"Empty definition is not possible","Error",1);
        }
        else
        {
            this.reloadPsp_man(psp);
            jTabbedPane1.setSelectedComponent(psp_man);
            this.mile_man.initC();

        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CManager().setVisible(true);
            }
        });
    }
    
     public static CManager getInstance() {
		return instance;
	}

    protected GantTable gant;
    protected LoginManager login;
    protected ProjectManager p_man;
    protected PSPManager psp_man;
    protected MilestonManager mile_man;
    protected java.awt.Image icon;
    protected java.awt.Toolkit tools;    
    private javax.swing.ImageIcon tab1I=new javax.swing.ImageIcon(getClass().getResource("Icons/search.png"));    
    private javax.swing.ImageIcon tab3I=new javax.swing.ImageIcon(getClass().getResource("Icons/reserv.png"));
    private javax.swing.ImageIcon tab4I=new javax.swing.ImageIcon(getClass().getResource("Icons/options.png"));
    private javax.swing.ImageIcon tab5I=new javax.swing.ImageIcon(getClass().getResource("Icons/clock.png"));
    private static CManager instance = null;
    public boolean initiated = false;
    
    protected String project;
    protected String psp;
    protected String milestone;
    protected PSPHierarchy PSPHproject;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JTree Tree;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    protected javax.swing.JMenuItem jMenuItem1;
    protected javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
